﻿''' <summary>
''' 工具类
''' </summary>
''' <remarks></remarks>
Public Class UtilityClass

    ''' <summary>
    ''' 将日期类型转换成字符串
    ''' </summary>
    ''' <param name="dt"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetDateString(ByVal dt As DateTime) As String
        Return dt.ToString("yyyy-MM-dd HH:mm:ss")
    End Function

    ''' <summary>
    ''' 检查字节数组是否为全0或者全FF
    ''' </summary>
    ''' <param name="buf"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function BufferIsValidate(ByVal buf() As Byte) As Boolean
        If buf Is Nothing Then
            Return False
        End If

        Dim result As Boolean = False

        For i As Integer = 0 To buf.Length - 1
            If buf(i) <> 0 Then
                result = True
                Exit For
            End If
        Next

        If result = False Then
            result = False
            For i As Integer = 0 To buf.Length - 1
                If buf(i) <> 255 Then
                    result = True
                    Exit For
                End If
            Next
        End If

        Return result

    End Function

    ''' <summary>
    ''' 验证数据是否为全0或者全1
    ''' </summary>
    ''' <param name="buf"></param>
    ''' <param name="startIndex"></param>
    ''' <param name="Length"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function BufferIsValidate(ByVal buf() As Byte, ByVal startIndex As Integer, ByVal Length As Integer) As Boolean
        If buf Is Nothing Then
            Return False
        End If

        If startIndex + Length - 1 > buf.Length Then
            'Stop
            Return False
        End If

        Dim result As Boolean = False

        For i As Integer = startIndex To startIndex + Length - 1
            If buf(i) <> 0 Then
                result = True
                Exit For
            End If
        Next

        If result = True Then
            result = False
            For i As Integer = startIndex To startIndex + Length - 1
                If buf(i) <> 255 Then
                    result = True
                    Exit For
                End If
            Next
        End If

        Return result

    End Function

    Public Shared Function GetAuthCodeByte(ByVal code As String) As Byte()
        Dim strByte As String
        Dim bsAuth() As Byte
        Dim bsEnd() As Byte

        bsAuth = System.Text.Encoding.ASCII.GetBytes("AT^AUTH=")
        bsEnd = System.Text.Encoding.ASCII.GetBytes(vbCrLf)

        Dim bytes(bsAuth.Length + code.Length / 2 + bsEnd.Length - 1) As Byte

        Array.Copy(bsAuth, 0, bytes, 0, bsAuth.Length)

        Dim bsKey(code.Length / 2 - 1) As Byte
        For i As Integer = 0 To code.Length - 2 Step 2
            strByte = code.Substring(i, 2)
            bsKey(i / 2) = Convert.ToByte(strByte, 16)
        Next

        Array.Copy(bsKey, 0, bytes, bsAuth.Length, bsKey.Length)
        Array.Copy(bsEnd, 0, bytes, bsAuth.Length + bsKey.Length, bsEnd.Length)

        Return bytes
    End Function

    Public Shared Function IsNotNullEmpty(ByVal obj As Object) As Boolean
        If obj IsNot Nothing And IsDBNull(obj) = False Then
            If TypeOf obj Is String Then
                If obj.Length > 0 Then
                    Return True
                End If
            Else
                Return True
            End If
        End If

        Return False
    End Function

End Class
